﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.WebApi.article
{
    [AuthPostAction("article", "publish", "1.0")]
    public class PublishAction : BaseAction<PublishParams, PublishResult>
    {
        protected override ActionResult<PublishResult> OnExecute(ActionContext context, SysParams sysParams, PublishParams opParams)
        {
            CMS.Domain.Services.PublishService service = new Domain.Services.PublishService();

            string htmlDirectory = System.Web.HttpContext.Current.Server.MapPath("~/articles/");
            string templateFile = System.Web.HttpContext.Current.Server.MapPath("~/_templates/article.html");
            string relativeHtmlDirectory = "./articles/";

            DateTime dateTime = DateTime.Now;

            DateTime? displayPublishedTime = null;
            if(DateTime.TryParse(opParams.DisplayPublishedTime, out dateTime))
            {
                displayPublishedTime = dateTime;
            }
            CPPEI.ServiceResult serResult = service.PublishArticle(opParams.Id, relativeHtmlDirectory, htmlDirectory, templateFile, displayPublishedTime);

            if(serResult.IsSuccess)
            {
                PublishResult actResult = new PublishResult();
                return new ActionResult<PublishResult>(actResult);
            }
            else
            {
                return new ActionResult<PublishResult>(serResult);
            }
        }
    }

    public class PublishParams
    {
        public Guid Id { get; set; }
        public string DisplayPublishedTime { get; set; }
    }

    public class PublishResult
    {
    }
}
